import request from '@/api/request.js'
const state={
    token:uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo').token : null,
    userInfo: uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo').info : null,
    isLogin: uni.getStorageSync('userInfo') ? true : false,
	uid:uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo').info.mid : null,
	add_id:null,
	workArr:[]
}

const mutations ={
	token(state,data){
		state.token = data;
	},
	userInfo(state,data){
		state.userInfo = data;
	},
	isLogin(state,data){
		state.isLogin = data;
	},
	uid(state,data){
		state.uid = data;
	},
	ADDID(state,data){
		state.add_id = data
	},
	workArr(state,data){
		state.workArr = data
	}
}

const actions={
// 登陆
	login({commit,dispatch,state},parms){
	    return new Promise((resolve,reject)=>{
	        request('/mLogin',{account:parms.account,password:parms.password}).then(res=>{
				if(res.code == 1 && res.data.info.source != 0){
					uni.setStorage({
						key : "userInfo",
						data:res.data
					})
					state.userInfo = res.data.info
					state.isLogin = true
					state.token = res.data.token
					state.uid = res.data.info.mid
					state.workArr = []
				}else{
					uni.showToast({
						icon:"none",
						title:res.msg
					})
				}
				resolve(res)
	        }).catch(err=>{
				uni.showToast({
					icon:"none",
					title:err.msg
				})
	            reject(err) 
	        })
	    })
	},


	//修改密码
	editPassword({commit,dispatch,state},parms){
	    return new Promise((resolve,reject)=>{
	        request('/editPassword',{mid:state.uid,old_pass:parms.old_pass,new_pass:parms.new_pass}).then(res=>{
				uni.showToast({
					icon:"none",
					title:res.msg
				})
				resolve(res)
	        }).catch(err=>{
				uni.showToast({
					icon:"none",
					title:err.msg
				})
	            reject(err) 
	        })
	    })
	},
	//退出登陆
	logout({commit,dispatch,state},parms){
	    return new Promise((resolve,reject)=>{
	        request('/mLogout',{mid:state.uid}).then(res=>{
				if(res.code == 1){
					uni.removeStorageSync("userInfo")
					state.userInfo = null
					state.isLogin = false
					state.token = null
					state.mid = null
				}else{
					uni.showToast({
						icon:"none",
						title:res.msg
					})
				}
				resolve(res)
	        }).catch(err=>{
				uni.showToast({
					icon:"none",
					title:err.msg
				})
	            reject(err) 
	        })
	    })
	},
	//负责区域列表
	areaList({commit,dispatch,state},parms){
	    return new Promise((resolve,reject)=>{
	        request('/areaList',{mid:state.uid}).then(res=>{
				resolve(res)
	        }).catch(err=>{
				uni.showToast({
					icon:"none",
					title:err.msg
				})
	            reject(err) 
	        })
	    })
	},
	//我的团队
	myTeam({commit,dispatch,state},parms){
	    return new Promise((resolve,reject)=>{
	        request('/myTeam',{mid:state.uid,qu_id:parms.qu_id,time:parms.time}).then(res=>{
				resolve(res)
	        }).catch(err=>{
				uni.showToast({
					icon:"none",
					title:err.msg
				})
	            reject(err) 
	        })
	    })
	},
	//查看服务人员详细信息
	serverData({commit,dispatch,state},parms){
	    return new Promise((resolve,reject)=>{
	        request('/serverData',{mid:state.uid,sp_id:parms.sp_id}).then(res=>{
				resolve(res)
	        }).catch(err=>{
				uni.showToast({
					icon:"none",
					title:err.msg
				})
	            reject(err) 
	        })
	    })
	},
	//编辑人员资料获取基本信息
	getServerInfo({commit,dispatch,state},parms){
		return new Promise((resolve,reject)=>{
			request('/getServerInfo',{mid:state.uid,sp_id:parms.sp_id}).then(res=>{
				resolve(res)
			}).catch(err=>{
				uni.showToast({
					icon:"none",
					title:err.msg
				})
				reject(err) 
			})
		})
	},
	//新增服务人员
	addServer({commit,dispatch,state},parms){
		return new Promise((resolve,reject)=>{
			request('/addServer',{
					mid:state.uid,
					imgurl:parms.imgurl,
					person_name:parms.person_name,
					working:parms.working,
					bind_serve:parms.bind_serve,
					bind_area:parms.bind_area,
					workexp:parms.workexp
				}).then(res=>{
				resolve(res)
			}).catch(err=>{
				uni.showToast({
					icon:"none",
					title:err.msg
				})
				reject(err) 
			})
		})
	},
	//更新人员资料
	updateServerInfo({commit,dispatch,state},parms){
		return new Promise((resolve,reject)=>{
			request('/updateServerInfo',{
					mid:state.uid,
					sp_id:parms.sp_id,
					person_name:parms.person_name,
					imgurl:parms.imgurl,
					working:parms.working,
					bind_serve:parms.bind_serve,
					bind_area:parms.bind_area
				}).then(res=>{
				resolve(res)
			}).catch(err=>{
				uni.showToast({
					icon:"none",
					title:err.msg
				})
				reject(err) 
			})
		})
	},
	//新增工作经历
	addWorkExp({commit,dispatch,state},parms){
	    return new Promise((resolve,reject)=>{
	        request('/addWorkExp',{mid:state.uid,sp_id:parms.sp_id,startTime:parms.startTime,endTime:parms.endTime,content:parms.content}).then(res=>{
				resolve(res)
	        }).catch(err=>{
				uni.showToast({
					icon:"none",
					title:err.msg
				})
	            reject(err) 
	        })
	    })
	},
	//删除工作经历
	delWorkExp({commit,dispatch,state},parms){
	    return new Promise((resolve,reject)=>{
	        request('/delWorkExp',{mid:state.uid,ex_id:parms.ex_id}).then(res=>{
				resolve(res)
	        }).catch(err=>{
				uni.showToast({
					icon:"none",
					title:err.msg
				})
	            reject(err) 
	        })
	    })
	},
	getAllArea({commit,dispatch,state},parms){
	    return new Promise((resolve,reject)=>{
	        request('/getAllArea',{mid:state.uid}).then(res=>{
				resolve(res)
	        }).catch(err=>{
				uni.showToast({
					icon:"none",
					title:err.msg
				})
	            reject(err) 
	        })
	    })
	},
	/* 消息 */
	 mOrderStatus({commit,dispatch,state},parms){
	     return new Promise((resolve,reject)=>{
	         request('/mOrderStatus',{mid:state.uid}).then(res=>{
	    resolve(res)
	         }).catch(err=>{
	    uni.showToast({
	     icon:"none",
	     title:err.msg
	    })
	             reject(err) 
	         })
	     })
	 },
}

const getters ={

}
export default {
    state,
    mutations,
    actions,
	getters
}